package com.grao.waterschool.human.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.grao.waterschool.human.common.R;
import com.grao.waterschool.human.entiry.dto.CourseHistoryDto;
import com.grao.waterschool.human.entiry.dto.CourseHistoryListDto;
import com.grao.waterschool.human.entiry.dto.GetCourseHistory;
import com.grao.waterschool.human.entiry.po.SchoolCourseHistory;
import com.grao.waterschool.human.entiry.vo.AvgInfo;
import com.grao.waterschool.human.entiry.vo.CourseHistoryVO;
import com.grao.waterschool.human.entiry.vo.HistoryUpdateInfo;
//import com.grao.waterschool.human.manager.service.CourseHistoryListService;
import com.grao.waterschool.human.manager.service.CourseHistoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

@Tag(name = "巡课历史查询接口")
@RestController
@RequestMapping("/schoolHistory/")
@Slf4j
public class CourseHistoryController {

    @Autowired
    private CourseHistoryService courseHistoryService;

    @SaCheckPermission(orRole = {"super-admin, admin"})
    @Operation(summary = "插入课程历史")
    @Parameter(name = "insertCourseHistory")
    @PostMapping("insert")
    public R insertCourseHistory(@RequestBody CourseHistoryDto courseHistoryDto) {
        return courseHistoryService.insertCourseHistory(courseHistoryDto);
    }


    @Parameter(name = "collegeList", description = "将需要查看平均数值的院系部名字作为参数传入")
    @Operation(summary = "根据院系名字查看平均数值")
    @PostMapping("avg")
    public R<List<AvgInfo>> selectCourseHistory(@RequestBody CourseHistoryDto courseHistoryDto) {
        List<AvgInfo> avgBycollege = courseHistoryService.getAvgBycollege(courseHistoryDto);
        return R.success(avgBycollege);
    }

    @Operation(summary = "删除课程历史")
    @Parameter(name = "courseHistoryId", description = "courseHistoryId")
    @DeleteMapping("delete")
    public R deleteCourseHistory(@RequestParam("courseHistoryId") String courseHistoryId) {
        return courseHistoryService.deleteCourseHistory(courseHistoryId);
    }

    @SaCheckPermission(orRole = {"super-admin, admin"})
    @Operation(summary = "课程历史查询")
    @Parameter(name = "getCourseHistory", description = "ZC、JSH、JGH、")
    @PostMapping("select")
    public R getCourseHistoryList(@RequestBody GetCourseHistory getCourseHistory) {
        Page<SchoolCourseHistory> courseHistoryPage = courseHistoryService.getCourseHistory(getCourseHistory);
        return R.success(courseHistoryPage);
    }

    @SaCheckPermission(orRole = {"super-admin, admin"})
    @Operation(summary = "课程历史导出")
    @Parameter(name = "getCourseHistory", description = "ZC、JSH、JGH")
    @PostMapping("output")
    public void outPutCourseHistoryList(@RequestBody GetCourseHistory getCourseHistory, HttpServletResponse response) throws IOException {
        log.info("getCourseHistoryList: {}", getCourseHistory);
        courseHistoryService.outPutCourseHistory(getCourseHistory, response);
    }

    @SaCheckPermission(orRole = {"super-admin, admin"})
    @Operation(summary = "根据图片名字下载图片")
    @Parameter(name = "picName", description = "picName")
    @PostMapping("getPicByName")
    public void getPicByName(@RequestParam("picName") String picName, HttpServletResponse response) throws Exception {

        courseHistoryService.getPicObject(picName, response);
    }

    @SaCheckPermission(orRole = {"super-admin, admin"})
    @Operation(summary = "更新课程历史")
    @Parameter(name = "historyUpdateInfo", description = "historyUpdateInfo")
    @PostMapping("update")
    public R updateCourseHistory(@RequestBody CourseHistoryListDto courseHistoryListDto) {
        return courseHistoryService.updateCourseHistory(courseHistoryListDto);
    }

    @Parameter(name = "collegeList", description = "查询手动巡检记录")
    @Operation(summary = "查询手动巡检记录")
    @PostMapping("list")
    public R getCourseHistoryByType(@RequestBody GetCourseHistory getCourseHistory) {
        Page<SchoolCourseHistory> page = courseHistoryService.getCourseHistoryByType(getCourseHistory);
        return R.success(page);
    }

    @Parameter(name = "courseHistoryDetail", description = "查询巡检详情")
    @Operation(summary = "查询巡检详情")
    @PostMapping("courseHistoryDetail")
    public R courseHistoryDetail(@RequestParam("courseHistoryId") Integer courseHistoryId) {
        CourseHistoryVO courseHistoryVO = courseHistoryService.getCourseHistoryById(courseHistoryId);
        return R.success(courseHistoryVO);
    }
}
